<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Outbox extends ORM {
    
     /*
     * Outbox belongs to message and user, 
     * 
     */

    protected $_belongs_to = array(
                'message' => array('model' => 'message', 'foreign_key' => 'message_id'),
                'user'    => array('model' => 'user', 'foreign_key' => 'user_id')
    );
    
    public function get_outbox($user_id, $limit = null, $page = null, $word = null)
    {
        $temp_outboxes = $this->select('messages.subject',
                                       'messages.content_message',
                                       'messages.date_created',
                                       'user_profiles.name', 
                                       'user_profiles.surname', 
                                       'user_profiles.description', 
                                       'user_profiles.url',
                                       array(DB::expr('COUNT(inboxes.message_id)'),'user_amout'), 
                                       array('user_profiles.file_id','photo'),
                                       array('files.id','file_id'),
                                       array('files.name','file_name'),
                                       array('files.extension','file_extension'))                
                             ->join('messages')->on('messages.id','=','outboxes.message_id')
                             ->join('inboxes')->on('inboxes.message_id', '=', 'messages.id')
                             ->join('user_profiles')->on('inboxes.user_id', '=', 'user_profiles.user_id')
                             ->join('files','LEFT')->on('user_profiles.file_id', '=', 'files.id')
                             ->where('outboxes.deleted','=',0)
                             ->where('outboxes.folder','=',0)
                             ->where('outboxes.user_id', '=', $user_id)
                             ->group_by('inboxes.message_id')
                             ->order_by('outboxes.id','DESC');

        if ($limit)
        {
            $temp_outboxes->limit($limit);
            if ($page)
            {
                $offset = $limit * ($page - 1);
                $temp_outboxes->offset($offset);
            }
        }
        
        if($word)
        {
            $temp_outboxes->where_open()
                         ->where('messages.subject', 'LIKE', '%'.$word.'%')
                         ->or_where('messages.content_message', 'LIKE', '%'.$word.'%')
                     ->where_close();
        }
        
        $temp_outboxes = $temp_outboxes->find_all();
        
        $outboxes = array();
        foreach($temp_outboxes as $outbox)
        { 
            $outbox->photo = Media::get_file_path($outbox->file_name, 
                                            $outbox->file_id, 
                                            $outbox->file_extension, 
                                            true, 
                                            'mini_square', 
                                            false);
            $outboxes[] = $outbox;
        }
        return $outboxes;
        
    }
    
}